# coding = utf-8
import datetime

from rest_framework import serializers
from rest_framework.fields import SerializerMethodField
from rest_framework.serializers import ModelSerializer

from wxchat.models import SwipeImage, WxUserInfo, WxUnifiedOrderResult, WxPayResult, RechargeRecord, RechargeList, \
    UserAcountHistory


class SwipeImageSerializer(ModelSerializer):
    """
    图片轮播
    """
    class Meta:
        model = SwipeImage
        exclude = ['update_time', 'add_time']


class UserRoleSerializer(ModelSerializer):
    """微信角色"""
    class Meta:
        model = WxUserInfo
        fields = ['openid', 'nickname', 'name', 'telephone', 'subscribe']


class WxUserInfoSerializer(ModelSerializer):
    """
     公众号用户信息
    """
    sex_name = serializers.CharField(source='get_sex_display', read_only=True)
    staff_name = serializers.CharField(source='get_staff_display', read_only=True)

    class Meta:
        model = WxUserInfo
        exclude = ["update_time", "add_time"]


class WxUnifiedOrderResultSerializer(ModelSerializer):
    """
    微信统一下单结果
    """

    class Meta:
        model = WxUnifiedOrderResult
        fields = "__all__"


class WxPayResultSerializer(ModelSerializer):
    """
    微信支付结果
    """
    class Meta:
        model = WxPayResult
        fields = "__all__"


class RechargeRecordSerializer(ModelSerializer):
    """
    用户充值记录表
    """
    retailer_name = serializers.CharField(source="retailer.name", read_only=True)
    status_name = serializers.CharField(source="get_status_display", read_only=True)
    pay_time = serializers.DateTimeField(format="%Y-%m-%d %H:%M:%S", required=False)

    class Meta:
        model = RechargeRecord
        exclude = ["update_time", "add_time"]


class UserAcountHistorySerializer(ModelSerializer):
    """
    充电桩信息
    """
    class Meta:
        model = UserAcountHistory
        fields = "__all__"


class RechargeListSerializer(ModelSerializer):
    """
    充值金额设置
    """
    update_date = SerializerMethodField()

    class Meta:
        model = RechargeList
        exclude = ["update_time", "add_time"]

    def get_update_date(self, obj):
        return obj.update_time.strftime("%Y-%m-%d")



